﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DIS_WEB_Client.Helpers;
using DIS_Web_Client.DIS_WS_BusinessLayer;

namespace DIS_WEB_Client.ShoppingCartModule
{
    public class ShoppingCart
    {
        string ShoppingCartId { get; set; }
        public const string CartSessionKey = "CartId";

        public static ShoppingCart GetCart(HttpContextBase context)
        {
            var cart = new ShoppingCart();
            cart.ShoppingCartId = cart.GetCartId(context);
            return cart;
        }

        // Helper method to simplify shopping cart calls 
        public static ShoppingCart GetCart(Controller controller)
        {
            return GetCart(controller.HttpContext);
        }

        public void AddToCart(ItemDTO item)
        {
            // Get the matching cart and album instances 
            var cartItem = BusService.Instance.Cart_SelectByIDAndItem(ShoppingCartId, item.ID);

            if (cartItem == null)
            {
                // Create a new cart item if no cart item exists 
                cartItem = new CartDTO
                {
                    ItemID = item.ID,
                    CartId = ShoppingCartId,
                    Count = 1,
                    DateCreated = DateTime.Now
                };
                // Insert new cart
                BusService.Instance.Cart_Insert(cartItem);
            }
            else
            {
                // If the item does exist in the cart, then add one to the quantity 
                cartItem.Count ++;
                // Update cart
                BusService.Instance.Cart_Update(cartItem);
            }
        }

        public void AddToCart(ItemDTO item, int num)
        {
            // Get the matching cart and album instances 
            var cartItem = BusService.Instance.Cart_SelectByIDAndItem(ShoppingCartId, item.ID);
            // If the item does exist in the cart, then add one to the quantity 
            cartItem.Count = num;
            // Update cart
            BusService.Instance.Cart_Update(cartItem);
        }

        public bool RemoveFromCart(int recordid)
        {
            return BusService.Instance.Cart_Delete(ShoppingCartId, recordid);
        }

        public bool EmptyCart()
        {
            return BusService.Instance.Cart_Empty(ShoppingCartId);
        }

        public List<CartDTO> GetCartItems()
        {
            return BusService.Instance.Cart_SelectByCartID(ShoppingCartId).ToList();
        }

        public double GetTotal()
        {
            // Multiply album price by count of that album to get  
            // the current price for each of those albums in the cart 
            // sum all album price totals to get the cart total 
            var lstCart = BusService.Instance.Cart_SelectByCartID(ShoppingCartId);
            double total = 0;
            if (lstCart != null)
            {
                foreach (var item in lstCart)
                {
                    var buyItem = BusService.Instance.Item_SelectByID(item.ItemID);
                    total += (double)item.Count * CommonHelpers.CalcPromotionPrice(buyItem);
                }
            }
            return Math.Round(total, 2); 
        }

         // We're using HttpContextBase to allow access to cookies. 
        public string GetCartId(HttpContextBase context)
        {
            if (context.Session[CartSessionKey] == null)
            {
                if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
                {
                    context.Session[CartSessionKey] = context.User.Identity.Name;
                }
                else
                {
                    // Generate a new random GUID using System.Guid class 
                    Guid tempCartId = Guid.NewGuid();

                    // Send tempCartId back to client as a cookie 
                    context.Session[CartSessionKey] = tempCartId.ToString();
                }
            }
            return context.Session[CartSessionKey].ToString(); 
        }

        // When a user has logged in, migrate their shopping cart to 
        // be associated with their username 
        public void MigrateCart(string userName)
        {
            var shoppingCart = BusService.Instance.Cart_SelectByCartID(ShoppingCartId);

            foreach (CartDTO item in shoppingCart)
            {
                item.CartId = userName;
            }
        }
    }
}